package com.deng.maintenance.kpi.control
{
	import com.deng.common.model.CommonModelLocator;
	import com.deng.event.ReturnEvent;
	import com.deng.maintenance.model.MaintenanceModelLocator;
	import com.deng.util.StringTools;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class KPIController
	{
		public function KPIController()
		{
		}
		
					
			
		private function saveRst(rst:ResultEvent):void{
			Alert.show("保存成功");
			MaintenanceModelLocator.getInstance().kpiDictItemVo = null;
//			var rtEvt1:ReturnEvent=new ReturnEvent(ReturnEvent.searchKpiDictItemList);
//			rtEvt1.dispatch();
			CommonModelLocator.getInstance().progress.endRun();
			var rtEvt:ReturnEvent=new ReturnEvent(ReturnEvent.closeKpiFunctionTw);
			rtEvt.dispatch();
		}
		
		public function saveStandard(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				fenzhi:String,biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				formula:String)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveStandard(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				fenzhi,biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				formula);
		}
		public function updateStandard(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				fenzhi:String,biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				formula:String,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateStandard(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				fenzhi,biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				formula,kpi_id);
		}
		public function saveStandardSection(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhi:String,biaozhunzhi:String,
				targetType:int,arr:ArrayCollection,
				formula:String)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveStandardSection(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhi,biaozhunzhi,
				targetType,arr,
				formula);
		}
		public function updateStandardSection(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhi:String,biaozhunzhi:String,
				targetType:int,arr:ArrayCollection,
				formula:String,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateStandardSection(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhi,biaozhunzhi,
				targetType,arr,
				formula,kpi_id);
		}
		
		public function saveStandardMingxi(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection,
				formula:ArrayCollection)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveStandardMingxi(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhiP,detailOrgArr,
				formula);
		}
		public function updateStandardMingxi(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection,
				formula:ArrayCollection,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateStandardMingxi(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhiP,detailOrgArr,
				formula,kpi_id);
		}
		public function saveReward(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int,
				formula:String)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveReward(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				rewardDanwei,
				formula);
		}
		public function updateReward(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int,
				formula:String,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateReward(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				rewardDanwei,
				formula,kpi_id);
		}
		public function saveRewardSection(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				biaozhunzhi:String,
				targetType:int,arr:ArrayCollection,
				formula:String)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveRewardSection(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				biaozhunzhi,
				targetType,arr,
				formula);
		}
		public function updateRewardSection(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				biaozhunzhi:String,
				targetType:int,arr:ArrayCollection,
				formula:String,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateRewardSection(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				biaozhunzhi,
				targetType,arr,
				formula,kpi_id);
		}
		public function saveRewardMingxi(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection,
				formula:ArrayCollection)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveRewardMingxi(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhiP,detailOrgArr,
				formula);
		}
		public function updateRewardMingxi(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection,
				formula:ArrayCollection,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateRewardMingxi(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,
				fenzhiP,detailOrgArr,
				formula,kpi_id);
		}
		public function saveDeny(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int,
				formula:String,noDouble:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveDeny(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				rewardDanwei,
				formula,noDouble);
		}
		public function updateDeny(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int,
				formula:String,kpi_id:int,noDouble:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateDeny(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,				
				biaozhunzhi,
				differentScale,scalePoint,
				targetType,addType,
				rewardDanwei,
				formula,kpi_id,noDouble);
		}
		public function saveDenyInput(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.saveDenyInput(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP);
		}
		public function updateDenyInput(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,kpi_id:int)
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,saveRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.updateDenyInput(zhibiaomingchengP,xianshimingchengP,
				descriptionP,frequenceP,kpi_id);
		}
		
		
		
		
		
		
		public function getStandardMingxiTest(formulaArr:ArrayCollection,testValue:ArrayCollection):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardMingxiTest(formulaArr,testValue);
		}
		public function getStandardMingxiFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection):ArrayCollection{//返回值:zhibiaomingcheng,xianshimingcheng,
//				description,frequence,
//				fenzhi:String,arr2
				
//arr:ArrayCollection=		zhibiaomingchengTA,xianshimingchengTA,noDouble,
//				biaozhunzhi,targetTypeCB,
//				standardDifferentScale,standardScalePoint,standardAddType
			var formulaArr:ArrayCollection = new ArrayCollection();
			formulaArr.addItem(zhibiaomingchengP);
			formulaArr.addItem(xianshimingchengP);
			formulaArr.addItem(descriptionP);
			formulaArr.addItem(frequenceP);
			formulaArr.addItem(fenzhiP);
			var detailArr:ArrayCollection = new ArrayCollection();
			
			for(var i:int = 0; i < detailOrgArr.length; i++){
				var detailItemArray:ArrayCollection = new ArrayCollection();
				var ob:ArrayCollection = detailOrgArr.getItemAt(i) as ArrayCollection;
				var zhibiaomingcheng:String = ob.getItemAt(0) as String;
				var xianshimingcheng:String = ob.getItemAt(1) as String;
				var noDouble:String = ob.getItemAt(2) as String;
				var biaozhunzhi:String = ob.getItemAt(3) as String;
				var targetType:String = ob.getItemAt(4) as String;
				var differentScale:String = ob.getItemAt(5) as String;
				var scalePoint:String = ob.getItemAt(6) as String;
				var standardAddType:String = ob.getItemAt(7) as String;
				var danwei:String = "0";
				
				var s:String = "";
//				Alert.show("targetType:"+targetType+"  standardAddType"+standardAddType);
				var addTypeS:String = "";
				if(standardAddType == "1"){//增加
					addTypeS = "+";
				}else if(standardAddType == "2"){//减少
					addTypeS = "-";
				}else{
					addTypeS = "=";
				}
				
				var rtFormula:String = "";
				if(noDouble == "1"){//一次性 
					if(targetType == "1"){//高好
						rtFormula = " case when X < "+biaozhunzhi+"-"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; 
					}else if(targetType == "2"){//低好
						rtFormula = " case when X > "+biaozhunzhi+"+"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; ; 
					}else{
//						Alert.show("!!!!00000");
					}
				}else{//可反复扣分
					var targetS:String = "";
					if(targetType == "1"){//越高越好
						targetS = "-";
					}else{//越低越好
						targetS = "+";
					}
					rtFormula = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;		
								
				}
				detailItemArray.addItem(zhibiaomingcheng);
				detailItemArray.addItem(xianshimingcheng);
				detailItemArray.addItem(noDouble);
				detailItemArray.addItem(biaozhunzhi);
				detailItemArray.addItem(targetType);
				detailItemArray.addItem(differentScale);
				detailItemArray.addItem(scalePoint);
				detailItemArray.addItem(standardAddType);
				detailItemArray.addItem(rtFormula);		
				detailItemArray.addItem(danwei);	

				detailArr.addItem(detailItemArray);		
			}	
			
			formulaArr.addItem(detailArr);		
			return formulaArr;
		}
		
		public function getStandardSectionFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhi:String,biaozhunzhi:String,
				targetType:int,arr:ArrayCollection):String{
			var s:String = " case when X="+biaozhunzhi+" then "+fenzhi+" ";
			for(var i:int = 0; i < arr.length; i++){
				var ob:Array = arr.getItemAt(i) as Array;//small,big,addType,targetPoint
				var smallS:String = ob[0];
				var bigS:String = ob[1];
				var addType:String = ob[2];
				var targetPoint:String = ob[3];
				if(targetType == 1){//高好
					if(addType == "0"){
						s += " when X >= "+smallS+" and X < "+bigS+" then "+targetPoint+" ";
					}else if(addType == "1"){//+
						s += " when X >= "+smallS+" and X < "+bigS+" then ("+fenzhi+"+"+targetPoint+") ";
					}else if(addType == "2"){//-
						s += " when X >= "+smallS+" and X < "+bigS+" then ("+fenzhi+"-"+targetPoint+") ";
					}
					
				}else{//低好
					if(addType == "0"){
						s += " when X > "+smallS+" and X <= "+bigS+" then "+targetPoint+" ";
					}else if(addType == "1"){//+
						s += " when X > "+smallS+" and X <= "+bigS+" then ("+fenzhi+"+"+targetPoint+")";
					}else{//-
						s += " when X > "+smallS+" and X <= "+bigS+" then ("+fenzhi+"-"+targetPoint+") ";
					}
					
				}				
			}
			s += "else "+fenzhi+" end";
			return s;
		}
		
		public function getStandardSectionTest(formula:String,testValue:String):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		
		public function getStandardFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				fenzhi:String,biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int):String{
			
//			var addTypeS:String = "";//其实不需要，默认都是减少，如果增加，则改变”趋势“即可
//			if(addType == 1){//增加
//				addTypeS = "+";
//			}else if(addType == 2){//减少
//				addTypeS = "-";
//			}else{
//				addTypeS = "=";
//			}
			var rtS:String;
			if(addType == 0)//结果等于一个数
			{
				if(targetType == 1){//越高越好
					rtS = "case when X >= "+biaozhunzhi+" then "+fenzhi+" else "+scalePoint+" end"; 
				}else{//越低越好
					rtS = "case when X <= "+biaozhunzhi+" then "+fenzhi+" else "+scalePoint+" end"; 
				}
			}
			else
			{
				var targetS:String;
				if(targetType == 1){//越高越好
					targetS = "-";
				}else{//越低越好
					targetS = "+";
				}
				rtS = ""+fenzhi+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
				var s:String = StringTools.replaceAll(rtS," ","");
				rtS = "case when "+s+" >= "+fenzhi+" then "+fenzhi+" when "+s+" <= 0 then 0 else "+s+" end";
			}
			
			
//			Alert.show(rtS);
			return rtS;
		}
		
		private function flt(ft:FaultEvent):void{
			CommonModelLocator.getInstance().progress.endRun();
			Alert.show(ft.fault.getStackTrace());
		}
		private function standardTestRst(rst:ResultEvent):void{
			Alert.show("测算结果 ＝ "+rst.result as String);
		}
		public function getStandardTest(formula:String,testValue:String):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		public function getRewardMingxiTest(formulaArr:ArrayCollection,testValue:ArrayCollection):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardMingxiTest(formulaArr,testValue);
		}
		public function getRewardMingxiFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				fenzhiP:String,detailOrgArr:ArrayCollection):ArrayCollection{//返回值:zhibiaomingcheng,xianshimingcheng,
//				description,frequence,
//				fenzhi:String,arr2
				
//arr:ArrayCollection=		zhibiaomingchengTA,xianshimingchengTA,noDouble,
//				biaozhunzhi,targetTypeCB,
//				standardDifferentScale,standardScalePoint,standardAddType
			var formulaArr:ArrayCollection = new ArrayCollection();
			formulaArr.addItem(zhibiaomingchengP);
			formulaArr.addItem(xianshimingchengP);
			formulaArr.addItem(descriptionP);
			formulaArr.addItem(frequenceP);
			formulaArr.addItem(fenzhiP);
			var detailArr:ArrayCollection = new ArrayCollection();
			
			for(var i:int = 0; i < detailOrgArr.length; i++){
				var detailItemArray:ArrayCollection = new ArrayCollection();
				var ob:ArrayCollection = detailOrgArr.getItemAt(i) as ArrayCollection;
				var zhibiaomingcheng:String = ob.getItemAt(0) as String;
				var xianshimingcheng:String = ob.getItemAt(1) as String;
				var noDouble:String = ob.getItemAt(2) as String;
				var biaozhunzhi:String = ob.getItemAt(3) as String;
				var targetType:String = ob.getItemAt(4) as String;
				var differentScale:String = ob.getItemAt(5) as String;
				var scalePoint:String = ob.getItemAt(6) as String;
				var standardAddType:String = ob.getItemAt(7) as String;
				var danwei:String = ob.getItemAt(8) as String;
				
				var s:String = "";
//				Alert.show("targetType:"+targetType+"  danwei "+danwei);
				var addTypeS:String = "";
				if(standardAddType == "1"){//增加
					addTypeS = "+";
				}else if(standardAddType == "2"){//减少
					addTypeS = "-";
				}else{
					addTypeS = "=";
				}
				
				var rtFormula:String = "";
				if(noDouble == "1"){//一次性 
					if(targetType == "1"){//高好
						rtFormula = " case when X < "+biaozhunzhi+"-"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; 
					}else if(targetType == "2"){//低好
						rtFormula = " case when X > "+biaozhunzhi+"+"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; ; 
					}else{
//						Alert.show("!!!!00000");
					}
				}else{//可反复扣分
					var targetS:String = "";
					if(targetType == "1"){//越高越好
						targetS = "-";
					}else{//越低越好
						targetS = "+";
					}
					rtFormula = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;		
								
				}
				detailItemArray.addItem(zhibiaomingcheng);
				detailItemArray.addItem(xianshimingcheng);
				detailItemArray.addItem(noDouble);
				detailItemArray.addItem(biaozhunzhi);
				detailItemArray.addItem(targetType);
				detailItemArray.addItem(differentScale);
				detailItemArray.addItem(scalePoint);
				detailItemArray.addItem(standardAddType);
				detailItemArray.addItem(rtFormula);		
				detailItemArray.addItem(danwei);
				
				detailArr.addItem(detailItemArray);		
			}	
			
			formulaArr.addItem(detailArr);		
			return formulaArr;
		}
		public function getRewardSectionFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				biaozhunzhi:String,
				targetType:int,arr:ArrayCollection):String{
			var s:String = " case when X="+biaozhunzhi+" then 0 ";
			for(var i:int = 0; i < arr.length; i++){
				var ob:Array = arr.getItemAt(i) as Array;//small,big,addType,targetPoint
				var smallS:String = ob[0];
				var bigS:String = ob[1];
				var addType:String = ob[2];
				var targetPoint:String = ob[3];
				if(targetType == 1){//高好
					if(addType == "0"){
						s += " when X >= "+smallS+" and X < "+bigS+" then "+targetPoint+" ";
					}else if(addType == "1"){//+
						s += " when X >= "+smallS+" and X < "+bigS+" then (+"+targetPoint+") ";
					}else if(addType == "2"){//-
						s += " when X >= "+smallS+" and X < "+bigS+" then (-"+targetPoint+") ";
					}
					
				}else{//低好
					if(addType == "0"){
						s += " when X > "+smallS+" and X <= "+bigS+" then "+targetPoint+" ";
					}else if(addType == "1"){//+
						s += " when X > "+smallS+" and X <= "+bigS+" then (+"+targetPoint+")";
					}else{//-
						s += " when X > "+smallS+" and X <= "+bigS+" then (-"+targetPoint+") ";
					}
					
				}				
			}
			s += "else 0 end";
			return s;
		}
		public function getRewardSectionTest(formula:String,testValue:String):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		
		
		
		public function getRewardStandardTest(formula:String,testValue:String):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		public function getRewardFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,
				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int):String{			
			var targetS:String = "";
			if(targetType == 1){//越高越好
				targetS = "-";
			}else{//越低越好
				targetS = "+";
			}
			//单项奖惩，如果是奖，就不会扣，如果是惩罚，就不会加。
			
			if(addType == 1){//增加,奖励
				var rtS:String = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
				var s:String = StringTools.replaceAll(rtS," ","");
				rtS = " case when "+s+" <= 0 then 0 else "+s+" end";
			}else if(addType == 2){//减少 惩罚
				var rtS:String = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
				var s:String = StringTools.replaceAll(rtS," ","");
				rtS = " case when "+s+" >= 0 then 0 else "+s+" end";
			}else{
				var rtS:String = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
				var s:String = StringTools.replaceAll(rtS," ","");
				rtS = " case when "+s+" <= 0 then 0 else "+s+" end";
			}
			
//			Alert.show(rtS);
			return rtS;
		}
		
		
		
		
		
		
		
		
		public function getDenyInputTest(formula:String,testValue:String):void{
//			Alert.show(formula);
//			var ro:RemoteObject = new RemoteObject();
//			ro.destination = "KPIManager";
//			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
//			ro.addEventListener(FaultEvent.FAULT,flt);
//			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		public function getDenyInputFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String
				):String{
			var rtS = descriptionP;
//			var targetS:String = "";
//			if(targetType == 1){//越高越好
//				targetS = "-";
//			}else{//越低越好
//				targetS = "+";
//			}
////			var addTypeS:String = "";//其实不需要，默认都是减少，如果增加，则改变”趋势“即可
////			if(addType == 1){//增加
////				addTypeS = "+";
////			}else if(addType == 2){//减少
////				addTypeS = "-";
////			}else{
////				addTypeS = "=";
////			}
//			var rtS:String = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
//			rtS = StringTools.replaceAll(rtS," ","");
//			Alert.show(rtS);
			return rtS;
		}
		
		public function getDenyStandardTest(formula:String,testValue:String):void{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "KPIManager";
			ro.addEventListener(ResultEvent.RESULT,standardTestRst);
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.standardTest(StringTools.replaceAll(formula,"X",testValue));
		}
		public function getDenyFormula(
				zhibiaomingchengP:String,xianshimingchengP:String,
				descriptionP:String,frequenceP:String,				
				biaozhunzhi:String,
				differentScale:String,scalePoint:String,
				targetType:int,addType:int,
				rewardDanwei:int,noDouble:int):String{			
			var targetS:String = "";
			if(targetType == 1){//越高越好
				targetS = "-";
			}else{//越低越好
				targetS = "+";
			}
			var addTypeS:String = "";//其实不需要，默认都是减少，如果增加，则改变”趋势“即可
			if(addType == 1){//增加
				addTypeS = "+";
			}else if(addType == 2){//减少
				addTypeS = "-";
			}else{
				addTypeS = "=";
			}
			
			var rtFormula:String = "";
			if(noDouble == 1){//一次性 
				if(targetType == 1){//高好
					rtFormula = " case when X <= "+biaozhunzhi+"-"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; 
				}else{//低好
					rtFormula = " case when X >= "+biaozhunzhi+"+"+differentScale+" then "+addTypeS+scalePoint+" else 0 end "; ; 
				}
			}else{//可反复扣分
				var targetS:String = "";
				if(targetType == 1){//越高越好
					targetS = "-";
				}else{//越低越好
					targetS = "+";
				}
				rtFormula = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;	
			}
			
//			var rtS:String = ""+targetS+"("+biaozhunzhi+"-X"+")/"+differentScale+"*"+scalePoint;
//			rtFormula = StringTools.replaceAll(rtFormula," ","");
//			Alert.show(rtS);
			return rtFormula;
		}
	}
}